/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package chuiniurobot.message;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import java.util.HashMap;
import java.util.Map;
import org.apache.mina.core.session.IoSession;

/**
 *
 *  对家猜测的骰子
 * @author zhengji
 */
public class VSPlayerGuessNumberMessage extends BaseMessage{

    public VSPlayerGuessNumberMessage(IoSession ioSession, String requestJSONStr) {
        super(ioSession, requestJSONStr);
    }

    @Override
    public void execute() {
        JSONObject jsonObj=JSON.parseObject(this.requestJSONStr);
     
         int vsPlayerID=jsonObj.getIntValue("vsid");
        String guessData=jsonObj.getString("d");
       
        if(guessData !=null){
          String[] array=guessData.split(",");
        
          if(array.length==2){
             this.setCurrentGuessAmount(Integer.parseInt(array[0]));//对家猜测数量
        
             this.setCurrentGuessNumber(Integer.parseInt(array[1]));//对家猜测点数
             
             this.guessNumber();//开始计算猜测
           }
       
          }
          
        
    }
     /**
     * 猜测点数
     */
    protected void guessNumber() {
 
        boolean blFinished=false;//是否计算完成
        
        int currentGuessNum=this.getCurrentGuessNumber();//当前猜测点数
        int currentGuessAmount=this.getCurrentGuessAmount();//当前猜测数量
            
        int tempCount=this.getNumberCountInSelfDice(currentGuessNum);
         
         if(currentGuessAmount-tempCount>=4){
             blFinished=false;
         }else{
             
             if(currentGuessAmount<=2){
               //当前猜测总数小于等2，则直接总数递增1
              currentGuessAmount ++;
             
              blFinished=true;
             }else if(currentGuessAmount<=tempCount || currentGuessAmount - tempCount==2){//猜测的数量小于等于自己拥有的数量，或者猜测的数量只比自己多1时
                blFinished=true;

                currentGuessAmount ++;
                
                
             }else{
                 
                 blFinished=calcGuessNumber();
             }
         }   
        
        if(blFinished){
               //成功计算到猜测            
               this.setCurrentGuessAmount(currentGuessAmount);
               this.setCurrentGuessNumber(currentGuessNum);
               
               //发送猜测结果
               sendGuessNumber();
         }else{
            //开看结果
             sendOpenGuess();
        }
        
        
    
    }

    /**
     * 计算猜测骰子(第二步)
     * @return 
     */
    private boolean calcGuessNumber() {
       boolean blFinished=false;
       int currentGuessNum=this.getCurrentGuessNumber();//当前猜测点数
        int currentGuessAmount=this.getCurrentGuessAmount();//当前猜测数量
            
        int tempCount=this.getNumberCountInSelfDice(currentGuessNum);
        
             for(int i=currentGuessNum +1;i<=6;i++){//从大于当前数开始找到有相同数量的骰子
                tempCount=this.getNumberCountInSelfDice(i);
                
                if(tempCount >=currentGuessAmount || currentGuessAmount - tempCount<=2){
                    currentGuessNum =i;
                    blFinished=true;
                    break;
                }
            }
      
            if(!blFinished){
            for(int i= 1;i<currentGuessNum;i++){
                tempCount=tempCount=this.getNumberCountInSelfDice(i);
                
                if(tempCount >=currentGuessAmount ||  currentGuessAmount - tempCount<=1){
                     currentGuessNum =i;
                     currentGuessAmount ++;
                    blFinished=true;
                    break;
                }
                
            }
         }     
         
           if(blFinished){
               //成功计算到猜测
               
               this.setCurrentGuessAmount(currentGuessAmount);
               this.setCurrentGuessNumber(currentGuessNum);
           }
       return blFinished;
    }

  

    /**
     * 开看结果
     */
    private void sendOpenGuess() {
        
       int count1=this.getNumberCountInSelfDice(this.getCurrentGuessNumber());
       int count2=this.getNumberCountInVSPlayerDice(this.getCurrentGuessNumber());
       
       int guessCount=this.getCurrentGuessAmount();//当前猜测数量
       
       Map<String,String> map=new HashMap<String,String>();
       map.put("cmd", BaseMessage.OpenMatchCommand);
       map.put("id", String.valueOf(this.getPlayerId()));
       map.put("vsid", String.valueOf(this.getVSPlayerId()));
       
       if(guessCount<=(count1 + count2)){
           //
           map.put("win", "0");
       }else{
           //获胜
           map.put("win", "1");
       }
       
       this.sendMessage(map);
        
    }
  
}
